<template>
  <view class="deco-person-head-bar" :style="[innerStyle]">
    <zm-navbar :is-back="false" :border-bottom="false" gradient gradient-heigt="50">
      <view slot="left">
        <u-icon v-if="isShowScan" :name="useImage('/zuimei-film-oss/1.0.0/icons/icon-scan.png')" size="46rpx" @click="handleScan"></u-icon>
      </view>
      <view slot="right" class="flex">
        <view class="flex mr-36">
          <u-icon :name="useImage('/zuimei-film-oss/1.0.0/icons/icon-msg.png')" size="46rpx" @click="handleToView('pages/person/user-message/user-message')"></u-icon>
        </view>
        <u-icon :name="useImage('/zuimei-film-oss/1.0.0/icons/icon-setting.png')" size="46rpx" @click="handleToView('pages/person/user-setting/user-setting')"></u-icon>
      </view>
    </zm-navbar>
  </view>
</template>

<script>
import mixin from "@/decoration/mixins"
import { mapGetters } from "vuex"
export default {
  name: "deco-person-head-bar",
  mixins: [mixin],
  data() {
    return {
      mitt: null,
      count: 0
    }
  },
  computed: {
    ...mapGetters(["login", "userInfo", "personDeco", "webEvn", "webEnv"]),
    // 渠道类型
    channelType() {
      return this.personDeco?.config?.channelType
    },
    isShowScan() {
      let show = true
      // #ifdef WEB
      const evns = ["ysf"]
      show = evns.includes(this.webEvn) || this.webEnv == "wxWeb"
      // #endif
      return show
    }
  },
  mounted() {
    this.init()
  },
  methods: {
    init() {
      const parent = this.useParent("deco-main")
      if (parent) {
        this.mitt = parent.mitt
      }
    },
    handleToView(url) {
      this.toView({ url, login: true })
    },
    handleScan() {
      const handle = async (res) => {
        const result = await this.$api.card.qrCodeBind({ qrCode: res.result })
        if (result.bindType == "coupon") {
          uni.$setCache("couponBindTips", `已绑定${result.bindNum || 1}张券`)
          uni.$toView("pages/person/coupon-list/coupon-list")
        } else if (result.bindType == "card") {
          uni.$setCache("cardBindTips", `已绑定${result.bindNum || 1}张卡`)
          uni.$toView("pages/person/film-card-list/film-card-list")
        } else if (result.bindType == "batchCoupon") {
          uni.$setCache("couponBindTips", `已绑定${result.bindNum || 1}张券`)
          uni.$toView("pages/person/coupon-list/coupon-list")
        } else if (result.bindType == "couponCard") {
          uni.$setCache("couponBindTips", result.message)
          uni.$toView("pages/person/coupon-list/coupon-list")
        }
        this.mitt?.emit("deco-person-property-bar.refresh")
      }
      // #ifdef WEB
      uni.$auth.sdk.scanQRCode({
        success: (res) => handle(res)
      })
      // #endif
      // #ifndef WEB
      uni.scanCode({
        success: (res) => handle(res)
      })
      // #endif
    }
  }
}
</script>

<style lang="scss" scoped>
.deco-person-head-bar {
  flex: 1;
  display: flex;
  align-items: center;
}
</style>
